// WaterManag.cpp : implementation file
//

#include "stdafx.h"
#include "voldia.h"
#include "WaterManag.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWaterManag dialog


CWaterManag::CWaterManag(CWnd* pParent /*=NULL*/)
	: CDialog(CWaterManag::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWaterManag)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CWaterManag::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWaterManag)
	DDX_Control(pDX, IDC_WATER_YES, m_selYes);
	DDX_Control(pDX, IDC_WATER_NO, m_selNo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWaterManag, CDialog)
	//{{AFX_MSG_MAP(CWaterManag)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_WATER_YES, OnWaterYes)
	ON_BN_CLICKED(IDC_WATER_NO, OnWaterNo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWaterManag message handlers
void CWaterManag::loadWater(){
	RETCOND		retCnd;
	TAGIDX		tagIdx;
	TAGVALEX	tagVal;

	retCnd = GetTagIdx(TWN_WATER_LEVEL_DIG, &tagIdx);
	if(retCnd != R_NORMAL){
		MessageBox(_T("Cannot find tag \"WATER\" idx."), _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}

	retCnd = TagRead(&tagIdx, &tagVal);
	if(retCnd != R_NORMAL){
		MessageBox(_T("Error reading tag \"WATER\" value."), _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		this->m_selYes.SetCheck(1);
		this->m_selNo.SetCheck(0);
	}
	else{
		this->m_selYes.SetCheck(0);
		this->m_selNo.SetCheck(1);
	}

	this->UpdateData(false);
}

BOOL CWaterManag::OnInitDialog(){
	CDialog::OnInitDialog();
	
	this->loadWater();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CWaterManag::OnShowWindow(BOOL bShow, UINT nStatus){
	CDialog::OnShowWindow(bShow, nStatus);
	
	this->loadWater();
}

void CWaterManag::OnWaterYes(){
	RETSTAT		retSt;

	this->UpdateData(true);

	if(this->m_selYes.GetCheck() == 1){
		this->m_selNo.SetCheck(0);
		retSt = VotedTagWrite(TWN_WATER_LEVEL_DIG, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Cannot write tag \"WATER\"."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
	}else{
		this->m_selNo.SetCheck(1);
		retSt = VotedTagWrite(TWN_WATER_LEVEL_DIG, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Cannot write tag \"WATER\"."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
	}

	this->UpdateData(false);
}

void CWaterManag::OnWaterNo(){
	RETSTAT		retSt;

	this->UpdateData(true);

	if(this->m_selNo.GetCheck() == 1){
		this->m_selYes.SetCheck(0);
		retSt = VotedTagWrite(TWN_WATER_LEVEL_DIG, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Cannot write tag \"WATER\"."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
	}else{
		this->m_selYes.SetCheck(1);
		retSt = VotedTagWrite(TWN_WATER_LEVEL_DIG, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Cannot write tag \"WATER\"."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
	}	

	this->UpdateData(false);
}
